<?php
namespace app\controller;

use app\BaseController;
use think\facade\Cookie;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Admin extends Cookies
{
    public function index()
    {
        return View::fetch();
    }
    public function welcome(){
        return View::fetch();
    }
    public function fenlei(){
        // 获取所有一级分类
        $parent_categories = Db::table("class")
            ->where('parent_id', 0)
            ->order('sort', 'asc')
            ->select();
        
        $result = [];
        // 获取每个一级分类下的二级分类
        foreach($parent_categories as $parent) {
            $result[] = $parent;
            // 获取当前一级分类下的所有二级分类
            $sub_categories = Db::table("class")
                ->where('parent_id', $parent['id'])
                ->order('sort', 'asc')
                ->select();
            
            foreach($sub_categories as $sub) {
                $result[] = $sub;
            }
        }
        
        View::assign([
            'lists' => $result
        ]);
        return View::fetch();
    }
    public function fenleiadd(){
        if (Request::isPost()){
            $title = input('post.title');
            $icon = input('post.icon');
            $sort = input('post.sort');
            $show = input('post.show', '1');
            $pid = input('post.pid', '0');  // 获取父级ID
            $encrypt = input('post.encrypt', '0');  // 获取加密状态，默认0不加密
            
            $add = Db::table("class")->insert([
                'title' => $title,
                'icon' => $icon,
                'sort' => $sort,
                'show' => $show,
                'parent_id' => $pid,  // 保存父级ID
                'encrypt' => $encrypt  // 保存加密状态
            ]);
            
            if (empty($add)){
                echo json_encode(['code' => 1, 'msg' => '添加分类失败']);
                exit;
            }else{
                echo json_encode(['code' => 0, 'msg' => '添加分类成功']);
                exit;
            }
        }else{
            // 获取所有分类列表
            $lists = Db::table("class")
                ->where('parent_id', 0)  // 只获取顶级分类
                ->order('sort', 'asc')
                ->select();
            View::assign([
                'lists' => $lists,
                'pid' => 0  // 添加pid变量，默认为0
            ]);
            return View::fetch();
        }
    }
    public function fenleidit(){
        if (Request::isPost()){
            $id = input('post.id');
            $title = input('post.title');
            $icon = input('post.icon');
            $sort = input('post.sort');
            $show = input('post.show', '1');
            $parent_id = input('post.parent_id', '0');  // 新增parent_id参数
            $encrypt = input('post.encrypt', '0');  // 新增encrypt参数
            
            $updates = Db::table("class")->where('id', $id)->update([
                'title' => $title,
                'icon' => $icon,
                'sort' => $sort,
                'show' => $show,
                'parent_id' => $parent_id,
                'encrypt' => $encrypt
            ]);
            
            if (empty($updates)){
                echo json_encode(['code' => 1, 'msg' => '修改分类失败']);
                exit;
            }else{
                echo json_encode(['code' => 0, 'msg' => '修改分类成功']);
                exit;
            }
        }else{
            $id = input('get.id');
            $finds = Db::table("class")->where('id', $id)->find();
            // 获取所有一级分类，用于选择父分类
            $parent_categories = Db::table("class")
                ->where('parent_id', 0)
                ->where('id', '<>', $id)  // 排除当前分类，防止自己成为自己的父分类
                ->order('sort', 'asc')
                ->select();
            View::assign([
                'finds' => $finds,
                'parent_categories' => $parent_categories
            ]);
            return View::fetch();
        }
    }
    public function fenleidel(){
        $id=input('get.id');
       $deletes = Db::table('class')->where('id',$id)->delete();
        if (empty($deletes)){
            echo json_encode(['code' => 1, 'msg' => '删除分类失败']);
            exit;
        }else{
            echo json_encode(['code' => 0, 'msg' => '删除分类成功']);
            exit;
        }
    }
    public function daohang(){
        $id=input('get.id');
        if(empty($id)){
            // 先获取所有一级分类下的导航 分类为空的情况下导致前端先显示全部再跳第一分类了
            /* $parent_nav = Db::query("SELECT data.*, class.parent_id FROM class INNER JOIN data ON data.class=class.id WHERE class.parent_id=0 ORDER BY class.sort,data.sort");
            
            $lists = [];
            // 获取每个分类下的导航
            foreach($parent_nav as $parent) {
                $lists[] = $parent;
                // 获取子分类下的导航
                $sub_nav = Db::query("SELECT data.*, class.parent_id FROM class INNER JOIN data ON data.class=class.id WHERE class.parent_id = " . $parent['class'] . " ORDER BY class.sort,data.sort");
                foreach($sub_nav as $sub) {
                    $lists[] = $sub;
                }
            } */
            $lists = [];
        }else{
            // 判断当前分类是否为一级分类
            $current_class = Db::table('class')->where('id', $id)->find();
            if($current_class && $current_class['parent_id'] == 0) {
                // 如果是一级分类，获取自己的导航和子分类的导航
                $lists = [];
                // 获取当前分类的导航
                $current_nav = Db::query("SELECT data.*, class.parent_id FROM class INNER JOIN data ON data.class=class.id WHERE data.class = " . $id . " ORDER BY data.sort");
                foreach($current_nav as $nav) {
                    $lists[] = $nav;
                }
                // 获取子分类的导航
                $sub_nav = Db::query("SELECT data.*, class.parent_id FROM class INNER JOIN data ON data.class=class.id WHERE class.parent_id = " . $id . " ORDER BY class.sort,data.sort");
                foreach($sub_nav as $sub) {
                    $lists[] = $sub;
                }
            } else {
                // 如果是子分类，只获取该分类下的导航
                $lists = Db::query("SELECT data.*, class.parent_id FROM class INNER JOIN data ON data.class=class.id WHERE data.class = " . $id . " ORDER BY data.sort");
            }
        }

        $class = Db::table("class")->order('sort','asc')->select();
        View::assign([
            'lists'=>$lists,
            'class'=>$class,
            'id'=>$id
        ]);
        return View::fetch();
    }
    public function daohangadd(){
        if (Request::isPost()){
            $class=input('post.class');
            $title=input('post.title');
            $mtitle=input('post.mtitle');
            $url=input('post.url');
            $icon=input('post.icon');
            $sort=input('post.sort');
            $show=input('post.show','1');
            $add=Db::table("data")->insert([
                'class' => $class,
                'title' => $title,
                'mtitle' => $mtitle,
                'url' => $url,
                'icon' => $icon,
                'sort' => $sort,
                'show' => $show
            ]);
            if (empty($add)){
                echo json_encode(['code' => 1, 'msg' => '添加导航失败']);
                exit;
            }else{
                echo json_encode(['code' => 0, 'msg' => '添加导航成功']);
                exit;
            }
        }else{
            $fenlei=input('get.fenlei');
            $paixu=input('get.paixu');
            
            // 获取所有分类，并按照层级排序
            $parent_categories = Db::table("class")
                ->where('parent_id', 0)
                ->order('sort', 'asc')
                ->select();
            
            $result = [];
            // 获取每个一级分类下的二级分类
            foreach($parent_categories as $parent) {
                $result[] = $parent;
                // 获取当前一级分类下的所有二级分类
                $sub_categories = Db::table("class")
                    ->where('parent_id', $parent['id'])
                    ->order('sort', 'asc')
                    ->select();
                
                foreach($sub_categories as $sub) {
                    $result[] = $sub;
                }
            }
            
            View::assign([
                'fenlei'=>$fenlei,
                'paixu'=>$paixu,
                'lists'=>$result,
            ]);
            return View::fetch();
        }
    }
    public function daohangdit(){
        if (Request::isPost()){
            $id=input('post.id');
            $class=input('post.class');
            $title=input('post.title');
            $mtitle=input('post.mtitle');
            $url=input('post.url');
            $icon=input('post.icon');
            $sort=input('post.sort');
            $show=input('post.show','1');
            $updates=Db::table("data")->where('id',$id)->update([
                'class' => $class,
                'title' => $title,
                'mtitle' => $mtitle,
                'url' => $url,
                'icon' => $icon,
                'sort' => $sort,
                'show' => $show
            ]);
            if (empty($updates)){
                echo json_encode(['code' => 1, 'msg' => '修改导航失败']);
                exit;
            }else{
                echo json_encode(['code' => 0, 'msg' => '修改导航成功']);
                exit;
            }
        }else{
            $id=input('get.id');
            $finds=Db::table("data")->where('id',$id)->find();
            
            // 获取所有分类，并按照层级排序
            $parent_categories = Db::table("class")
                ->where('parent_id', 0)
                ->order('sort', 'asc')
                ->select();
            
            $result = [];
            // 获取每个一级分类下的二级分类
            foreach($parent_categories as $parent) {
                $result[] = $parent;
                // 获取当前一级分类下的所有二级分类
                $sub_categories = Db::table("class")
                    ->where('parent_id', $parent['id'])
                    ->order('sort', 'asc')
                    ->select();
                
                foreach($sub_categories as $sub) {
                    $result[] = $sub;
                }
            }
            
            View::assign([
                'finds'=>$finds,
                'lists'=>$result
            ]);
            return View::fetch();
        }
    }
    public function daohangdel(){
        $id=input('get.id');
        $deletes = Db::table('data')->where('id',$id)->delete();
        if (empty($deletes)){
            echo json_encode(['code' => 1, 'msg' => '删除导航失败']);
            exit;
        }else{
            echo json_encode(['code' => 0, 'msg' => '删除导航成功']);
            exit;
        }
    }
    public function user(){
        if (Request::isPost()){
            $username=input('post.username');
            $password=input('post.password');
            $pass1=input('post.pass1');
            $pass2=input('post.pass2');
            if($pass1 != $pass2){
                echo json_encode(['code' => 1, 'msg' => '两次密码不相等']);
                exit;
            }
            $re=Db::table("user")->where([
                'username'=>$username,
                'password'=>$password
            ])->find();
            if (empty($re)){
                echo json_encode(['code' => 1, 'msg' => '原密码错误']);
                exit;
            }else{
                $updates=Db::table("user")->where('username',$username)->update([
                    'password'=>$pass1
                ]);
                echo json_encode(['code' => 0, 'msg' => '修改密码成功']);
                exit;
            }
        }else{
            return View::fetch();
        }
    }
    public function set(){
        if (Request::isPost()){
            $title=input('post.title');
            $notice=input('post.notice');
            /* $icon=input('post.icon'); */
            $footer=input('post.footer');
            $logo1=input('post.logo1');
            $logo2=input('post.logo2');
            $password=input('post.password');
            // 获取百度翻译相关字段
            $baidu_translate_show=input('post.baidu_translate_show_value', '0');
            $baidu_translate_encrypt=input('post.baidu_translate_encrypt_value', '0');
            $baidu_app_id=input('post.baidu_app_id');
            $baidu_secret_key=input('post.baidu_secret_key');
            
            $updates=Db::table("set")->where('setid','1')->update([
                'title'=>$title,
                'notice'=>$notice,
                /* 'icon'=>$icon, */
                'footer'=>$footer,
                'logo1'=>$logo1,
                'logo2'=>$logo2,
                'password'=>$password,
                // 添加百度翻译字段的更新
                'baidu_translate_show'=>$baidu_translate_show,
                'baidu_translate_encrypt'=>$baidu_translate_encrypt,
                'baidu_app_id'=>$baidu_app_id,
                'baidu_secret_key'=>$baidu_secret_key
            ]);
            if (empty($updates)){
                echo json_encode(['code' => 1, 'msg' => '修改设置失败']);
                exit;
            }else{
                echo json_encode(['code' => 0, 'msg' => '修改设置成功']);
                exit;
            }
        }else{
            $finds=Db::table("set")->where('setid','1')->find();
            View::assign([
                'finds'=>$finds
            ]);
            return View::fetch();
        }
    }
    public function uplogo(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file);
        //$savename = \think\facade\Filesystem::putFile( 'topic', $file);
        $savename=str_replace('\\','/',$savename);
        //$savename  =》upload/20210725/28b20fe2b1913c94df7f84a239156fd7.png
        $savename='/storage/'.$savename;
        return json(['code'=>0,'imgPath'=>$savename,'msg'=>'上传成功']);
    }
    public function upicon(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 上传到本地服务器
        $info = $file->move('../public/','favicon.ico');
        //halt( $info);
        if($info){
            $res['code']=0;
            $res['msg'] = '上传成功！';
//            $res['name'] = $info->getFilename();
//            $res['imgPath'] = 'upload/admin/'.$info->getSaveName();
        }else{
            $res['code'] = 1;
            $res['msg'] = '上传失败！'.$file->getError();
        }
        return $res;

    }
}
